Positioning
in Streams
AVIFile
provides several ways to locate and move to a position in a data stream. The
functions and macros in this section let your application find the starting
position, length, and key frames (containing a complete image in the sample)
within a stream. The functions and macros also associate time with positions in
a stream by calculating the elapsed time needed to play a stream from its
beginning to any point in a stream.
Finding the Starting Position
You can
retrieve the sample number of the first frame in a video stream by using the AVIStreamStart
You can retrieve
the stream length by using the AVIStreamLength_PG.32 function. This function returns the number
of samples in the stream. You can also obtain this information by using the AVIStreamInfo
function. This function stores the stream length in the dwLength member
of the AVISTREAMINFO structure. To retrieve the length of a stream in
milliseconds, use the AVIStreamLengthTime macro.
In a video
stream, each sample generally corresponds to a frame of video. There might,
however, be samples for which no video data is present. If you call the AVIStreamReadJ2Q.K function and specifying the FIND_ANY flag.
In an audio
stream, each sample corresponds to one data block of audio data. For example,
if the audio data has a 22 kHz ADPCM (Adaptive Differential Pulse Code
Modulation) format, each sample for AVIStreamLength corresponds to a
block of 256 bytes of compressed audio data. This block of audio data contains
approximately 500 audio samples when uncompressed. The functions and macros of
AVIFile, however, treat each 256-byte block as a single sample.
Note Valid positions
within a stream range from the beginning to the end of the stream, which is the
sum of the stream starting point and its length. The position represented by
the sum of the starting position and the length corresponds to a time after the
last data has been rendered; it does not contain any data. You can retrieve the
sample number that represents the end of the stream by using the AVIStreamEnd macro. You can retrieve the
time value in milliseconds that represents the end of the stream by using the AVIStreamEndTime macro.
Finding Sample and Key Frames
You can
search for different types of samples in a stream by using the AVIStreamFindSample
Several
macros that use AVIFile functions supplement the stream search features. The
following list provides a brief description of each macro. The macros that
search for a specific position or type of data require a starting location to
be specified in the stream.
Macro |
Description |
AVIStreamIsKeyFrame |
Indicates
whether a sample in a specified stream is a key frame. |
AVIStreamNearestKeyFrame |
Locates the
key frame at or preceding a specified position in a stream. |
AVIStreamNearestKeyFrameTime |
Determines
the time corresponding to the beginning of the key frame nearest (at or
preceding) a specified time in a stream. |
AVIStreamNearestSample |
Locates the
nearest nonempty sample at or preceding a specified position in a stream. |
AVIStreamNearestSampleTime |
Determines
the time corresponding to the beginning of a sample that is nearest to a
specified time in a stream. |
AVIStreamNextKeyFrame |
Locates the
next key frame following a specified position in a stream. |
AVIStreamNextKeyFrameTime |
Returns the
time of the next key frame in a stream, starting at a given time. |
AVIStreamNextSample |
Locates the
next nonempty sample from a specified position in a stream. |
AVIStreamNextSampleTime |
Returns the
time that a sample changes to the next sample in the stream. |
AVIStreamPrevKeyFrame |
Locates the
key frame that precedes a specified position in a stream. |
AVIStreamPrevKeyFrameTime |
Returns the
time of the previous key frame in the stream, starting at a given time. |
AVIStreamPrevSample |
Locates the
nonempty sample that precedes a specified position in a stream. |
AVIStreamPrevSampleTime |
Determines
the time at which the previous sample replaces its predecessor in the stream. |
AVIStreamSampleToSample |
Returns the
sample in a stream that occurs at the same time as a sample that occurs in a
second stream. |
Switching Between Samples and Time
You can
determine the elapsed time from the beginning of a stream to a sample using the
AVIStreamSampleToTime
Conversely,
you can find the sample number associated with a time value by using the AVIStreamTimeToSample
Because AVIStreamSampleToTime
returns the time at which a frame begins to play, the relationship between AVIStreamSampleToTime
and AVIStreamTimeToSample is not truly inverse. They determine the
position in a file more acurately than they determine time. For example, two
consecutive audio samples might both play in the same millisecond. Using AVIStreamSampleToTime
to convert the sample numbers would result in identical time values. If you
convert the time value back to a sample number by using AVIStreamTimeToSample,
a single sample would be referenced.